

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class backt {

	int select[];
	int best[];
	int splitnum;
	int turlnum;
	int gun=0;
	StringBuilder html=null;
	String turl;
	String key;//="ȫ�浿";
	String http;//="http://chulic.cheju.ac.kr//DLiWeb20fr/components/searchir/result.aspx?";
	String str[]; //= {"m_var=421","srv_id=31","qy_frm=QUICK","qy_typ=KEYWORD","qy_idx=TITL","qy_kwd=%ED%99%8D%EA%B8%B8%EB%8F%99","brch=ALL"};
	backt(String str, String key, String http, String strstr[])
	{
		this.turl=str;
		this.key = key;
		this.http= http+"?";
		this.str=strstr;
		this.splitnum=strstr.length;
		select = new int[strstr.length];
		best= new int[strstr.length];
		html = new StringBuilder(); 
		try {
			URL url = new URL(turl);
			HttpURLConnection conn = null; 
            if (url.getProtocol().toLowerCase().equals("https")) { 
            } else { 
            	conn = (HttpURLConnection) url.openConnection(); 
            }
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				int resultcode = conn.getResponseCode();
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));	
					for (;;) {						
						String line = br.readLine();				
						if (line == null) break;
						html.append(line + '\n'); 
					}
					br.close();
				}
				conn.disconnect();
			}
		} 
		catch (Exception ex) {
			}
		this.turlnum=findkwd(html.toString(),key,0);
	}
	public void bt(int deep, int lay, int select[])
	{
		gun++;
		System.out.println("실행횟수: "+gun);
		System.out.println("깊이: "+deep);
		int how[]=new int[select.length];
		System.arraycopy(select,0,how,0,select.length);
		int num = count(how);
		System.out.println("num: "+num);
		if (deep > lay)
		{
			return;
		}
		if(pro(how)) //&& splitnum >= num)
		{
			if(splitnum >= num)
			{
				splitnum = num;
				for(int j=0; j < how.length ; j++)
					best[j] = how[j];
			}
			bt(deep+1, lay, how);
			if(deep!=lay)
				how[deep]=1;
			bt(deep+1, lay, how);
		}
	}
	public boolean pro(int how[])
	{
		/*
	      if(findkwd(sb.toString(),key,0)==findkwd(sb2.toString(),key,0))
	    	  return true;
	      else
	    	  return false;*/
		StringBuilder html2 = new StringBuilder(); 

		try {
			URL url2 = new URL(hap(how));
			
			System.out.println(hap(how));
			
			HttpURLConnection conn2 = null; 
            if (url2.getProtocol().toLowerCase().equals("https")) { 
            } else { 
            	conn2 = (HttpURLConnection) url2.openConnection(); 
            } 		
			if (conn2 != null) {
				conn2.setConnectTimeout(10000);
				conn2.setUseCaches(false);
				int resultcode2 = conn2.getResponseCode();
				if (conn2.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br2 = new BufferedReader(new InputStreamReader(conn2.getInputStream(),"UTF-8"));					
					for (;;) {					
						String line2 = br2.readLine();
						if (line2 == null) break;
						html2.append(line2 + '\n'); 
					}
					br2.close();
				}
				conn2.disconnect();
			}
		}
		catch (Exception ex) {
		}
		
		
		/*
		StringBuilder html = new StringBuilder(); 
		try {
			URL url = new URL(turl);
			HttpURLConnection conn = null; 
            if (url.getProtocol().toLowerCase().equals("https")) { 
            } else { 
            	conn = (HttpURLConnection) url.openConnection(); 
            }
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				int resultcode = conn.getResponseCode();
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));	
					for (;;) {						
						String line = br.readLine();				
						if (line == null) break;
						html.append(line + '\n'); 
					}
					br.close();
				}
				conn.disconnect();
			}
		} 
		catch (Exception ex) {
			}
*/

		//System.out.println(findkwd(html.toString(),key,0));
		
		System.out.println(turlnum+" => "+findkwd(html2.toString(),key,0));
		System.out.println("------------");
		if(findkwd(html2.toString(),key,0)==turlnum)
			return true;
		else
			return false;
	}
	
	public int count(int how[])
	{
		int con=0;
		for(int i = 0 ; i < how.length; i++)
			if(how[i]==0)
				con++;
		return con;
	}
	public String hap(int select[])
	{
		String sol= http;
		for(int i = 0 ; i < select.length; i++)
			if(select[i]==0){
				//System.out.println(select[i]);
				sol=sol+"&"+str[i];
			}
		return sol;
	//	System.out.println(sol);
	}
	public String hap2()
	{
		String sol= http;
		for(int i = 0 ; i < best.length; i++)
			if(best[i]==0){
				//System.out.println(select[i]);
				sol=sol+"&"+str[i];
			}
		return sol;
	//	System.out.println(sol);
	}
	public int findkwd(String turl, String kwd, int indexnum  )
	{
		int k,count=0;
		k=turl.indexOf(kwd, indexnum);
		while(k!=-1)
		{
			count++;
			k=turl.indexOf(kwd,k+1);
		}
		return count;
	}
	public int gun()
	{
		return gun;
	}
}
